package controller;

import com.google.gson.Gson;
import pojo.Employees;
import pojo.Salaries;
import pojo.SalaryDTO;
import service.IBusinessManagementService;
import service.ISalaryAdministrationService;
import service.imppl.BusinessManagementService;
import service.imppl.SalaryAdministrationService;
import util.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@WebServlet("/salaryControl/*")
public class SalaryController extends HttpServlet {
    private ISalaryAdministrationService isas=new SalaryAdministrationService();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        int index = requestURI.lastIndexOf("/") + 1;
        String mName = requestURI.substring(index);
        if (mName.length() == 0) {
            mName = "showdt";
        }
        Class cla = this.getClass();
        Method method = null;
        try {
            request.setCharacterEncoding("utf-8");
            response.setContentType("application/json;charset=utf-8");
            method = cla.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    public void salaryList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        Salaries salaries = new Salaries();
        PageHelper ph = new PageHelper();
        Employees employees = new Employees();
        String eName = request.getParameter("eName");
        String departmentStr=request.getParameter("department");
        int department;
        if(departmentStr.equals("")){
            department=-1;
        }else {
            department=Integer.parseInt(departmentStr);
        }
        System.out.println(department);
        employees.setDepartmentId(department);
        employees.seteName(eName);
        int totalCount = isas.queryCountSalaries(salaries, employees);
        System.out.println(totalCount);
        ph.setDateCount(totalCount);
        int indexInt = 1;
        try {
            indexInt = Integer.parseInt(request.getParameter("indexPage"));
        } catch (NumberFormatException e) {
        }
        ph.setPageIndex(indexInt);
        ArrayList<SalaryDTO> salaryList = isas.querySalaries(salaries, employees, ph);
        ArrayList<SalaryDTO> list=new ArrayList();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String startDateStr=request.getParameter("startTime");
            String endDateStr=request.getParameter("endDate");
            if (startDateStr != null && !startDateStr.trim().isEmpty()&&endDateStr != null && !endDateStr.trim().isEmpty()) {
                Date startDate = formatter.parse(startDateStr);
                Date endDate = formatter.parse(endDateStr);
                for (int i = 0; i < salaryList.size(); i++) {
                    if(salaryList.get(i).getEffectiveDate().before(endDate)||salaryList.get(i).getEffectiveDate().after(startDate)){
                        list.add(salaryList.get(i));
                    }
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        ph.setListIndex(list);
        response.getWriter().println(new Gson().toJson(ph));
    }
}
